  /**
    * 图片button类
    * by dily
    * (c) copyright 2014 - 2035
    * All Rights Reserved. 
    * 可以有图片，文字，动画
    * todo:九宫格、多动画、图字等
    */

class EToggleButton extends egret.DisplayObjectContainer{

    private textField:egret.TextField;
    private assets:egret.SpriteSheet = RES.getRes("assets");//名称不一样的话需要修改
    private btnImg:egret.Bitmap;
    private backFun:Function;
    private isPlayCartoon:Boolean = false;
    private cartoonType:number = 1;
    private isSelected:Boolean = false;
    private imgNormalName:string = "";
    private imgSelectName:string = "";
    private param = {context:null,data:null};//回调参数
    /**
    * context             上下文环境
    * imgNormalName       图片
    * imgSelectName       图片
    * backFun       点击方法 如果需要在backFun中使用this的，小心使用这个
    * descStr       按钮描述
    * fontSize      字体大小
    * cartoonType   动画类型 1:【可爱】按下变小，放开弹大 2:按下变小，放开轻微弹大 3：按下变小，放开变大
    * 注意：如果有动画的话，只有动画结束才会触发click事件
    */
    public constructor(context:any,imgNormalName:string,imgSelectName:string,backFun:Function = null,descStr:string = "",fontSize:number = 30,cartoonType:number = 1,assetsName:string = "assets"){
        super();
        this.param.context = context;
        this.imgNormalName = imgNormalName;
        this.imgSelectName = imgSelectName;
        this.init(backFun,descStr,fontSize,cartoonType,assetsName);
    }

    private init(backFun:Function = null,descStr:string = "",fontSize:number = 30,cartoonType:number = 1,assetsName:string = "assets"):void {
        this.cartoonType = cartoonType;
        this.backFun = backFun;
        this.btnImg = new egret.Bitmap();
        if(assetsName != "assets"){
            this.assets = RES.getRes(assetsName);
        }
        this.btnImg.texture = this.assets.getTexture(this.imgNormalName);
        this.addChild(this.btnImg);   

        if(descStr != ""){
            this.textField = new egret.TextField();
            this.addChild(this.textField);
            this.textField.size = fontSize;
            this.textField.textAlign = "center";
            this.textField.bold = true;
            this.textField.stroke = 1;
            this.textField.strokeColor = 0x665249;
            this.textField.textColor = 0xbdb2aa;
            this.textField.text = descStr;
            this.textField.fontFamily = "微软雅黑";
            this.textField.width = this.btnImg.width;
            this.textField.x = this.btnImg.width/2 - this.textField.width/2;
            this.textField.y = this.btnImg.height/2 - this.textField.height/2;
        }
        this.touchEnabled = true;

        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onbuttonTouchTap, this);
    }

    private onbuttonTouchTap(e):void {
        if(this.isPlayCartoon){
            return;
        }
        this.isPlayCartoon = true;
        var onComplete2:Function = function(){//改变状态
            this.isPlayCartoon = false;
        }; 
        var onComplete1:Function = function(){
            if(this.isSelected){
                this.btnImg.texture = this.assets.getTexture(this.imgNormalName);
            }else{
                this.btnImg.texture = this.assets.getTexture(this.imgSelectName);
            }
            this.isSelected = !this.isSelected;

            if(this.cartoonType == 1){
                egret.Tween.get(this).to({scaleX:1,scaleY:1,x:this.x - this.btnImg.width/4,y:this.y - this.btnImg.height/4},500,egret.Ease.elasticOut).call(onComplete2,this); 
            }else if(this.cartoonType == 2){
                egret.Tween.get(this).to({scaleX:1,scaleY:1,x:this.x - this.btnImg.width/4,y:this.y - this.btnImg.height/4},500,egret.Ease.backOut).call(onComplete2,this); 
            }else if(this.cartoonType == 3){
                egret.Tween.get(this).to({scaleX:1,scaleY:1,x:this.x - this.btnImg.width/4,y:this.y - this.btnImg.height/4},100).call(onComplete2,this); 
            }
        };   
        egret.Tween.get(this).to({scaleX:0.5,scaleY:0.5,x:this.x + this.btnImg.width/4,y:this.y + this.btnImg.height/4},100,egret.Ease.sineIn).call(onComplete1,this);            

        egret.setTimeout(function () {
            if(this.backFun != null){
                this.backFun.apply(this.param.context, [this.param.data]);
            } 
        }, this, 200); 
    }

    public getBitmap():egret.Bitmap{
        return this.btnImg;
    }

    //设置绑定数据
    public setBindData(data):void{
        this.param.data = data;
    }

    //获取绑定数据
    public getBindData():any{
        return this.param.data;
    }

    //获取是否选择
    public getSelected():Boolean{
        return this.isSelected;
    }

    //设置按钮是否可用
    public setEnable(bool:boolean):void{
        this.touchEnabled = bool;
    }

    //设置是否选择
    public setSelected(bool):void{
        this.isSelected = bool;
        if(bool){
            this.btnImg.texture = this.assets.getTexture(this.imgSelectName);
        }else{
            this.btnImg.texture = this.assets.getTexture(this.imgNormalName);
        }        
    }
}
